package com.keur.money.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.databinding.DialogMullaSignRiseBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.event.KeurEventType
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：签到提额提示dialog
 */
class SignRiseDialog: ProDialog {

    private lateinit var binding: DialogMullaSignRiseBinding
    private var mCallback: IDialogCallback? = null

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaSignRiseBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose)//关闭
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
    }

    /**
     * 设置签到提额内容
     */
    @SuppressLint("SetTextI18n")
    fun showMullaSignRiseInfo(data: JSONObject){
        val pName = data.optString("name")
        val showText = String.format(MyApp.app.getString(R.string.dialog_sign_rise_limit_increased),pName)
        val startIndex = showText.indexOf(pName)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#ffed6700")), startIndex, startIndex + pName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + pName.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textSignRiseLimitIncreased.text = spString

        //提额额度
        binding.textSignRiseAmount.text = "${data.optString("signRiseAmount")} ${data.optString("currency")}"
        //下次签到提额额度
        val nextSignRiseAmount = data.optString("nextSignRiseAmount")
        if (nextSignRiseAmount.isEmpty()){
            binding.textSignRiseAmountTomorrow.visibility = View.GONE
        } else {
            binding.textSignRiseAmountTomorrow.visibility = View.VISIBLE

            val showNextSignRiseText = String.format(MyApp.app.getString(R.string.dialog_sign_rise_limit_tomorrow),nextSignRiseAmount)
            val startNextSignRiseIndex = showNextSignRiseText.indexOf(nextSignRiseAmount)
            val nextSignRiseSpString = SpannableString(showNextSignRiseText)
            nextSignRiseSpString.setSpan(ForegroundColorSpan(Color.parseColor("#FF8027FF")), startNextSignRiseIndex, startNextSignRiseIndex + nextSignRiseAmount.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            nextSignRiseSpString.setSpan(StyleSpan(Typeface.BOLD), startNextSignRiseIndex, startNextSignRiseIndex + nextSignRiseAmount.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
            binding.textSignRiseAmountTomorrow.text = nextSignRiseSpString
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
        fun onDismiss()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_SIGN_RISE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        mCallback?.onDismiss()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_SIGN_RISE_DISMISS)
    }
}